const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});
//========================
//业务逻辑
module.exports = function() {
    var router = express.Router();
    router.get("/updatePrimaryClassification.html",function(req,res,next){
        //判断是否登陆
        if(req.session['userId']==null||req.session['userId']=="")
        {
            res.status(400).send('<script>alert("请登录");window.location.href="login.html";</script>').end();
        }
        var classAId=+req.query.classAId;
            db.query("SELECT * FROM classa WHERE classAId="+classAId+"",function(err,data){
                    if(err)
                    {
                         res.status(500).send('<script>alert("出错啦");</script>').end();
                    }else
                    {
                        if(data.length==0)
                        {
                            res.status(400).send('<script>alert("没有数据!");window.location.reload();</script>').end();
                        }else {
                           req.cclassANamecont=data;
                        }
                        next();
                    }
            })
    });

//修改
    router.use("/updat",function(req,res,next){

        //获取url参数
        var classAId=urlobj.parse(req.url,true).query.classAId;

                db.query("UPDATE classa SET classAName='"+req.query.TMuername+"' WHERE classAId="+req.query.updateid+"",function(err,data) {
                    if(err)
                    {
                        res.status(500).send('<script>alert("sql修改失败!");window.location.href="404.html";</script>').end()
                    }else
                    {
                        if(data.length=0)
                        {
                            res.status(300).send('<script>alert("修改失败!");window.location.href="updatePrimaryClassification.html";</script>').end()
                        }else
                        {
                            res.status(200).send('<script>alert("修改成功!");window.location.href="PrimaryClassification.html";</script>').end()
                        }
                        next();
                    }
                })
    });
    router.get('/updatePrimaryClassification.html',function(req,res,next){
         res.render("admin/updatePrimaryClassification.ejs",{Nameconts:req.cclassANamecont});
    });
    return router;
}